//
//  loginViewController.m
//  projectOfTeamSample
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "loginViewController.h"
#import "loginView.h"
#import "RegisterViewController.h"
#import <AVUser.h>
#import "UserDetaileTableViewController.h"
#import "GetBackViewController.h"
@interface loginViewController ()

@property (nonatomic,strong)loginView *loginview;

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.loginview = [[loginView alloc]initWithFrame:self.view.frame];
    [self.loginview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"log"]]];
    [self.loginview.loginButton addTarget:self action:@selector(didClickLoginButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.loginview.registButton addTarget:self action:@selector(didClickRegistButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.loginview.getBackPasswordButton addTarget:self action:@selector(didClickgetBackButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.view = self.loginview;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)didClickLoginButtonAction:(UIButton *)send{
    
    AVUser *current = [AVUser currentUser];
    
    if (current != nil) {
        
    }else{
    
    [AVUser logInWithMobilePhoneNumberInBackground:self.loginview.userText.text password:self.loginview.passwordText.text  block:^(AVUser *user, NSError *error) {
        
        if (!error) {            
            [self.navigationController pushViewController:[[UserDetaileTableViewController alloc]init] animated:YES];
            return ;
        }
        else{
            [AVUser logInWithUsernameInBackground:self.loginview.userText.text password:self.loginview.passwordText.text block:^(AVUser *user, NSError *error) {
                if (!user) {
                    user = [AVUser user];
                    NSString *errorString = [NSString stringWithFormat:@"%@",error];
                    NSRange range = NSMakeRange(45, 38);
                    NSMutableString *string = (NSMutableString *)[errorString substringWithRange:range];
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"登录失败" message:string preferredStyle:(UIAlertControllerStyleAlert)];
                    UIAlertAction *re_registrationAction = [UIAlertAction actionWithTitle:@"重新登录" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    
                    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                        [self.navigationController pushViewController:[[UserDetaileTableViewController alloc]init]  animated:YES ];
                    }];
                    
                    [alertC addAction:re_registrationAction];
                    [alertC addAction:cancleAction];
                    
                    [self presentViewController:alertC animated:YES completion:nil];
                }else{
                    [self.navigationController pushViewController:[[UserDetaileTableViewController alloc]init] animated:YES];
                }
            }];
        }
        
    }];
        
    }
    
}

-(void)didClickRegistButtonAction:(UIButton *)send{
    [self.navigationController pushViewController:[[RegisterViewController alloc]init] animated:YES];
}

-(void)didClickgetBackButtonAction:(UIButton *)send{
    [self.navigationController pushViewController:[[GetBackViewController alloc]init] animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
